package l1j.server.server.clientpackets;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.Timestamp;
import java.util.List;

import l1j.opqlo.List.WeaponArmorLV;
import l1j.opqlo.List.特殊列表;
import l1j.opqlo.NewSystem.各項記錄輸出;
import l1j.opqlo.Server.Data.WeaponArmorLevelUp;
import l1j.opqlo.Server.L1World.L1World;
import l1j.opqlo.Server.L1World.WorldClan;
import l1j.server.echo.ClientThread;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.datatables.RaceTicketTable;
import l1j.server.server.datatables.ShopTable;
import l1j.server.server.model.L1Clan;
import l1j.server.server.model.L1Inventory;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1PcInventory;
import l1j.server.server.model.Instance.L1DummyInstance;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.model.item.L1ItemId;
import l1j.server.server.model.shop.L1Shop;
import l1j.server.server.model.shop.L1ShopBuyOrderList;
import l1j.server.server.model.shop.L1ShopSellOrderList;
import l1j.server.server.serverpackets.S_AddItem;
import l1j.server.server.serverpackets.S_DeleteInventoryItem;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.templates.L1Item;
import l1j.server.server.templates.L1PrivateShopBuyList;
import l1j.server.server.templates.L1PrivateShopSellList;
import l1j.server.server.templates.L1RaceTicket;
public class C_Result extends ClientBasePacket {

	private static final String C_RESULT = "[C] C_Result";
	private final StringBuilder msg = new StringBuilder(768);//opqlo 倉庫紀錄
	private static final Logger _log = Logger.getLogger(C_Result.class.getName());
	@Override
	public String getType() {
		return C_RESULT;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance pc = client.getActiveChar();
			if (pc == null || pc.isInHell()) {
				return;
			}
			final int npcObjectId = this.readD();
			final int resultType = this.readC();
			final int size = this.readC();
			this.readC();
			final int level = pc.getLevel();
			int npcId = 0;
			String npcImpl = "";
			boolean isPrivateShop = false;
			boolean tradable = true;
			final L1Object findObject = L1World.getInstance().findObject(npcObjectId);
			if (findObject != null) {
				final int diffLocX = Math.abs(pc.getX() - findObject.getX());
				final int diffLocY = Math.abs(pc.getY() - findObject.getY());
				// 3マス以上離れた場合アクション無効
				if (diffLocX > 3 || diffLocY > 3) {
					return;
				}
				if (findObject instanceof L1NpcInstance) {
					final L1NpcInstance targetNpc = (L1NpcInstance) findObject;
					npcId = targetNpc.getNpcTemplate().get_npcId();
					npcImpl = targetNpc.getNpcTemplate().getImpl();
				} else if (findObject instanceof L1PcInstance) {
					isPrivateShop = true;
				}
			}
			if(size == 0){
				if(resultType == 5 && npcImpl.equalsIgnoreCase("L1Dwarf")) { // 血盟倉庫使用中Cancel、離線、ESC
					final L1Clan clan = WorldClan.get().getClan(pc.getClanname());
					if (clan != null) {
						clan.setWarehouseUsingChar(0); // 解鎖血盟倉庫
					}
					return;
				}else{
					return;
				}
			}
			if (findObject instanceof L1PcInstance || findObject instanceof L1DummyInstance){
				isPrivateShop = true;
			}

			switch(resultType){
			case 0:
				/**NPC商店購入*/
			/*	if( npcImpl.equalsIgnoreCase("L1Dummy")){

					L1DummyInstance dummy = (L1DummyInstance) findObject;
					Shop<L1DummyInstance> shop = new Shop<L1DummyInstance>(pc, dummy);

					for (int i = 0; i < size; i++){
						shop.add(readD(), readD());
					}

					if (npcId >= 97302 && npcId <= 97305){
						shop.sell(50015);
						return;
					}else if (npcId >= 97306 && npcId <= 97311){
						shop.sell(npcId - 47289);
						return;
					}else if (npcId == 97312 || npcId == 97313){
						shop.sell(50016);
						return;
					}else{
						shop.sell(L1ItemId.ADENA);
						return;
					}
				}else */
					if(npcImpl.equalsIgnoreCase("L1Merchant")){
					/**opqlo 寄賣系統*/
					/*if(npcId == 700009){
						synchronized(AutoShopTable.getInstance()){
							if(pc.ShopListVersion != AutoShopTable.getInstance().GetVersion()){
								pc.sendPackets(new S_SystemMessage("購買清單已經過期，請重新跟NPC對話。"));
								return;
							}
							for (int i = 0; i < size; i++) {
								final 	int item = readD();
								int count = readD();
								count = Math.abs(count);
								count = Math.min(count,2000000000);
								count = Math.max(count,0);
								//count = Math.min(count,9999);
								AutoShopTable.getInstance().add(item, count);
							}
							AutoShopTable.getInstance().sellitem(pc);
						}
					}else*/ if(npcId == 600012){
						final L1PcInventory inv = pc.getInventory();
						final L1ItemInstance bb = inv.findItemId(90308);
						long logcount = 0;
						if(bb==null && pc._coverType == 99 && pc._coverType ==98){
							pc.sendPackets(new S_SystemMessage("身上元寶數量不足。"));
							return;
						}else if(bb != null){
							logcount = bb.getCount();
						}
						L1ItemInstance CoverItem  ;
						WeaponArmorLV temp = null ;
						boolean log = false;
						this.msg.append("時間:	").append(new Timestamp(System.currentTimeMillis())).append("\r\n\r\n")
						.append(pc.getNetConnection().getIp().toString()).append( "	玩家:【").append(pc.getName()).append("】\r\n")
						.append("----------").append("【轉換】清單如下:----------轉換前元寶: ").append(logcount).append("\r\n\r\n");
						if(pc._coverType == 99){
							for (int i = 0; i < size; i++) {
								final int item = this.readD();
								this.readD();
								CoverItem =inv.get轉換升級清單().get(item);
								final int itemid = CoverItem.getItemId();
								temp = WeaponArmorLevelUp.getInstance().getLvUpTemp(itemid);
								final int price  = temp.getLevelUpPrice();

								if(inv.consumeItem(90308 , price)){

									this.msg.append("[").append(itemid).append("] ").append(CoverItem.getViewName());
									pc.sendPackets(new S_DeleteInventoryItem(CoverItem));
									CoverItem.setItemId(temp.getNextItemId());
									final L1Item template = ItemTable.getInstance().getTemplate(CoverItem.getItemId());
									CoverItem.setItem(template);
									inv.saveItem(CoverItem, L1PcInventory.COL_ITEMID);
									pc.sendPackets(new S_AddItem( CoverItem));//opqlo 線上更新

									this.msg.append("				升級為: ").append("[").append(CoverItem.getItemId()).append("] ").append(CoverItem.getLogName())
									.append("			流水號:【  ").append(CoverItem.getId()).append(" 】\r\n");
									if(!log){
										log = true;
									}
								}else{
									pc.sendPackets(new S_SystemMessage("身上元寶數量不足。"));
								}
							}
							if(log){
								final L1ItemInstance item = inv.findItemId(90308);
								long count =0;
								if(item != null){
									count = item.getCount();
								}
								this.msg.append("轉換後元寶: ").append(count).append(" \r\n");
								各項記錄輸出.LogSave(23,this.msg.toString());
							}
						}else if(pc._coverType ==98){
							for (int i = 0; i < size; i++) {
								final int item = this.readD();
								this.readD();
								CoverItem =inv.get轉換升級清單().get(item);
								final int itemid = CoverItem.getItemId();
								temp = WeaponArmorLevelUp.getInstance().getLvUpTemp(itemid);
								final int price =temp.getBlessPrice();

								if(inv.consumeItem(90308 , price)){
									this.msg.append("[").append(itemid).append("] ").append(CoverItem.getViewName());
									pc.sendPackets(new S_DeleteInventoryItem(CoverItem));
									CoverItem.setBless(0);
									pc.getInventory().updateItem(CoverItem,L1PcInventory.COL_BLESS);
									pc.getInventory().saveItem(CoverItem,L1PcInventory.COL_BLESS);
									pc.sendPackets(new S_AddItem( CoverItem));//opqlo 線上更新

									this.msg.append("				轉祝福為流水號:【  ").append(CoverItem.getId()).append(" 】\r\n");
									if(!log){
										log = true;
									}
								}else{
									pc.sendPackets(new S_SystemMessage("身上元寶數量不足。"));
								}
							}
							if(log){
								final L1ItemInstance item = inv.findItemId(90308);
								long count =0;
								if(item != null){
									count = item.getCount();
								}
								this.msg.append("轉換後元寶: ").append(count).append(" \r\n");
								各項記錄輸出.LogSave(25,this.msg.toString());
							}
						}else if(pc._coverType >=0){
							for (int i = 0; i < size; i++) {
								final int item = this.readD();
								this.readD();
								CoverItem =inv.get轉換升級清單().get(item);
								final int itemid = CoverItem.getItemId();
								boolean isOK = false;
								long count = 0;
								if(bb !=null){
									count = bb.getCount();
								}
								if(CoverItem.getItem().getType2()==0){
									isOK = true;
								}else if(count >=500){
									inv.removeItem(bb, 500);
									isOK = true;
								}else{
									pc.sendPackets(new S_SystemMessage("身上元寶數量不足。"));
								}
								if(isOK){
									this.msg.append("[").append(itemid).append("] ").append(CoverItem.getViewName());
									pc.sendPackets(new S_DeleteInventoryItem(CoverItem));
									CoverItem.setItemId(特殊列表.get武器轉換編號(itemid , pc._coverType));
									final L1Item template = ItemTable.getInstance().getTemplate(CoverItem.getItemId());
									CoverItem.setItem(template);
									inv.saveItem(CoverItem, L1PcInventory.COL_ITEMID);
									pc.sendPackets(new S_AddItem( CoverItem));//opqlo 線上更新
									this.msg.append("				轉換為: ").append("[").append(CoverItem.getItemId()).append("] ").append(CoverItem.getLogName())
									.append("			流水號:【  ").append(CoverItem.getId()).append(" 】\r\n");
									if(!log){
										log = true;
									}
								}
							}
							if(log){
								final L1ItemInstance item = inv.findItemId(90308);
								long count =0;
								if(item != null){
									count = item.getCount();
								}
								this.msg.append("轉換後元寶: ").append(count).append(" \r\n");
								各項記錄輸出.LogSave(22,this.msg.toString());
							}
						}
						pc._coverType =-1;
						inv.Remove轉換升級清單();
						this.msg.delete(0, this.msg.length());
					}else{
						final L1Shop shop = ShopTable.getInstance().get(npcId);
						final 	L1ShopBuyOrderList orderList = shop.newBuyOrderList();
						for (int i = 0; i < size; i++) {

							final int item = this.readD();
							int count = this.readD();
							count = Math.abs(count);
							count = Math.max(count,0);
							count = Math.min(count,99999);
							if(count <=0){
								continue;
							}
							orderList.add(item,count);
							//opqlo 防洗
						}
						shop.sellItems(pc, orderList);
					}

					/**個人商店購入*/
				}else if (isPrivateShop) {
					if(findObject instanceof L1PcInstance == false){
						return;
					}
					int order;
					int count;
					int price;
					//ArrayList sellList;
					List<L1PrivateShopSellList> sellList;
					L1PrivateShopSellList pssl;
					int itemObjectId;
					int sellPrice;
					int sellTotalCount;
					int sellCount;
					L1ItemInstance item;
					final boolean[] isRemoveFromList = new boolean[8];

					final L1PcInstance targetPc  = (L1PcInstance) findObject;
					if (targetPc.isTradingInPrivateShop() || !targetPc.isPrivateShop()
							|| pc.getMapId() != targetPc.getMapId()) {
						return;
					}
					sellList = targetPc.getSellList();
					synchronized (sellList) {
						// 売り切れが発生し、閲覧中のアイテム数とリスト数が異なる
						if (pc.getPartnersPrivateShopItemCount() != sellList.size()) {
							return;
						}
						targetPc.setTradingInPrivateShop(true);
						this.msg.append("時間:	").append(new Timestamp(System.currentTimeMillis())).append("\r\n\r\n")
						.append(targetPc.getNetConnection().getIp().toString()).append( "	擺攤玩家:【").append(targetPc.getName()).append("】	身上元寶:").append(targetPc.getInventory().findItemIdAndGetCount(90308)).append("\r\n")
						.append(pc.getNetConnection().getIp().toString()).append( "	對象玩家:【").append(pc.getName()).append("】	身上元寶:").append(pc.getInventory().findItemIdAndGetCount(90308)).append("\r\n")
						.append("----------").append("【售出】清單如下:----------\r\n\r\n");
						boolean log =false;
						for (int i = 0; i < size; i++) { // 購入予定の商品
							order = this.readD();
							count = this.readD();
							//opqlo 防洗
							count = Math.abs(count);
							count = Math.min(count,2000000000);
							count = Math.max(count,0);
							//opqlo 防洗
							if(order > sellList.size()){
								continue;
							}
							pssl = sellList.get(order);
							itemObjectId = pssl.getItemObjectId();
							sellPrice = pssl.getSellPrice();
							sellTotalCount = pssl.getSellTotalCount(); // 売る予定の個数
							sellCount = pssl.getSellCount(); // 売った累計
							item = targetPc.getInventory().getItem(itemObjectId);
							if (item == null) {
								continue;
							}
							if (count > sellTotalCount - sellCount) {
								count = sellTotalCount - sellCount;
							}
							if (count == 0) {
								continue;
							}
							//opqlo 防洗
							if(count > 0 && count < 2000000000
									&& item.getCount() > 0 && item.getCount() >= count)
							{//opqlo 防洗
								if (pc.getInventory().checkAddItem(item, count) == L1Inventory.OK) { // 容量重量確認及びメッセージ送信
									for (int j = 0; j < count; j++) { // オーバーフローをチェック
										if (sellPrice * j > 2000000000) {
											pc.sendPackets(new S_ServerMessage(904,"2000000000"));// 総販売価格は%dアデナを超過できません。
											targetPc.setTradingInPrivateShop(false);
											return;
										}
									}
									price = count * sellPrice;
									if(price == 0){
										continue;
									}
									final int Money;
									if(pc.getMapId() == 370){ //opqlo 元寶掛賣區
										Money = 90308;
									}else{
										Money =L1ItemId.ADENA;
									}

									if (pc.getInventory().checkItem(Money, price)) {
										final L1ItemInstance adena = pc.getInventory().findItemId(Money);
										if (targetPc != null && adena != null) {
											if (targetPc.getInventory().tradeItem(item,count, pc.getInventory()) == null) {
												targetPc.setTradingInPrivateShop(false);
												return;
											}
											pc.getInventory().tradeItem(adena, price,targetPc.getInventory());
										
											this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName()).append("		售價:").append(price)
											.append("	流水號:【  ").append(item.getId()).append(" 】\r\n");
											if(!log){log=true;}
										
											targetPc.sendPackets(new S_ServerMessage(877,pc.getName(), item.getItem().getName()+ " (" + String.valueOf(count) + ")"));
											pssl.setSellCount(count + sellCount);
											sellList.set(order, pssl);
											if (pssl.getSellCount() == pssl.getSellTotalCount()) { // 売る予定の個数を売った
												isRemoveFromList[order] = true;
											}
										}
									} else {
										if(Money == 40308){
											pc.sendPackets(new S_ServerMessage(189)); // \f1アデナが不足しています。
										}else if(Money == 90308){
											pc.sendPackets(new S_SystemMessage("商城元寶不足。"));
										}
										break;
									}
								} else {
									pc.sendPackets(new S_ServerMessage(270)); // \f1持っているものが重くて取引できません。
									break;
								}
								//opqlo 防洗
							} else {
								_log.info("[c]Result 防洗踢除 "+client.getAccountName());
								client.close();
								continue;
							}
							//opqlo 防洗
						}
						// 売り切れたアイテムをリストの末尾から削除
						for (int i = 7; i >= 0; i--) {
							if (isRemoveFromList[i]) {
								sellList.remove(i);
							}
						}
						if(log){
							this.msg.append( "	擺攤玩家:【").append(targetPc.getName()).append("】	身上元寶:").append(targetPc.getInventory().findItemIdAndGetCount(90308)).append("\r\n")
							.append( "	對象玩家:【").append(pc.getName()).append("】	身上元寶:").append(pc.getInventory().findItemIdAndGetCount(90308)).append("\r\n");
							各項記錄輸出.LogSave(3,this.msg.toString());
						}
						targetPc.setTradingInPrivateShop(false);
					}
					this.msg.delete(0, this.msg.length());
				}
				break;
			case 1:
				/**NPC商店販售*/
				if(npcImpl.equalsIgnoreCase("L1Merchant")) {
					final 	L1Shop shop = ShopTable.getInstance().get(npcId);
					final 	L1ShopSellOrderList orderList = shop.newSellOrderList(pc);
					L1RaceTicket ticket;//opqlo 賭肥支票
					boolean Check =false;
					for (int i = 0; i < size; i++) {
						//opqlo 防洗orderList.add(readD(), readD());
						//opqlo 防洗
						final int item = this.readD();
						int count = this.readD();
						count = Math.abs(count);
						count = Math.min(count,2000000000);
						count = Math.max(count,0);

						ticket = RaceTicketTable.getInstance().getTemplate(item);//opqlo 賭肥支票

						if(npcId==70035||npcId==70041||npcId==70042){

							if(ticket ==null){ continue; }
							Check=true;
						}else{
							if(ticket !=null){
								continue;
							}
						}
						orderList.add(item,count);
						//opqlo 防洗
					}
					shop.buyItems(orderList,Check);
					//					shop.buyItems(orderList);


					/***個人商店販售*/
				} else if(isPrivateShop) {
					if(findObject instanceof L1PcInstance == false){
						return;
					}
					int count;
					int order;
					//ArrayList buyList;
					List<L1PrivateShopBuyList> buyList;
					L1PrivateShopBuyList psbl;
					int itemObjectId;
					L1ItemInstance item;
					int buyPrice;
					int buyTotalCount;
					int buyCount;
					final boolean[] isRemoveFromList = new boolean[8];

					final L1PcInstance targetPc =(L1PcInstance) findObject;


					if (targetPc.isTradingInPrivateShop()|| !targetPc.isPrivateShop()
							|| pc.getMapId() != targetPc.getMapId()) {
						return;
					}
					targetPc.setTradingInPrivateShop(true);
					buyList = targetPc.getBuyList();
					this.msg.append("時間:	").append(new Timestamp(System.currentTimeMillis())).append("\r\n\r\n")
					.append(targetPc.getNetConnection().getIp().toString()).append( "	擺攤玩家:【").append(targetPc.getName()).append("】	身上元寶:").append(targetPc.getInventory().findItemIdAndGetCount(90308)).append("\r\n")
					.append(pc.getNetConnection().getIp().toString()).append( "	對象玩家:【").append(pc.getName()).append("】	身上元寶:").append(pc.getInventory().findItemIdAndGetCount(90308)).append("\r\n")
					.append("----------").append("【收購】清單如下:----------\r\n\r\n");
					boolean log =false;
					int allPrice =0;
					int Money = L1ItemId.ADENA;
					L1ItemInstance adena = null;
					for (int i = 0; i < size; i++) {
						itemObjectId = this.readD();
						count = this.readCH();

						//opqlo 防洗
						count = Math.abs(count);
						count = Math.min(count,2000000000);
						count = Math.max(count,0);
						//opqlo 防洗

						order = this.readC();
						item = pc.getInventory().getItem(itemObjectId);
						if (item == null) {
							continue;
						}
						//opqlo 防洗
						if(count > 0 && count < 2000000000
								&& item.getCount() > 0 && item.getCount() >= count)
						{//opqlo 防洗

							psbl = buyList.get(order);
							buyPrice = psbl.getBuyPrice();
							buyTotalCount = psbl.getBuyTotalCount(); // 買う予定の個数
							buyCount = psbl.getBuyCount(); // 買った累計

							if (count > buyTotalCount - buyCount) {
								count = buyTotalCount - buyCount;
							}
							if (item.isEquipped()) {
								pc.sendPackets(new S_ServerMessage(905)); // 装備しているアイテムは販売できません。
								continue;
							}
							if (!item.getItem().isTradable()) {//opqlo 不能交易物品無法放入商店販售
								pc.sendPackets(new S_ServerMessage(166, // \f1%0が%4%1%3%2
										item.getItem().getName(), " 無法交易"));
								continue;
							}
							if (item.getDeleteDate() != null) { //opqlo 道具天數刪除系統(無法擺商店)
								pc.sendPackets(new S_ServerMessage(166, item.getName(), " 無法交易"));
								continue;
							}
							// 防止異常堆疊交易
							if (item.getCount() > 1 && item.getItem().isStackable() == false) {
								pc.sendPackets(new S_ServerMessage(166,"此物品非堆疊，但異常堆疊無法交易"));
								continue;
							}
							// 使用中的寵物項鍊 - 無法販賣
							for (final L1NpcInstance petNpc : pc.getPetList().values()) {
								if (petNpc instanceof L1PetInstance) {
									final L1PetInstance pet = (L1PetInstance) petNpc;
									if (item.getId() == pet.getItemObjId()) {
										pc.sendPackets(new S_ServerMessage(1187)); // 寵物項鍊正在使用中。
										continue;
									}}}
							// 使用中的魔法娃娃 - 無法販賣
							/*for (final L1DollInstance doll : pc.getDollList().values()) {
								if (doll.getItemObjId() == item.getId()) {
									pc.sendPackets(new S_ServerMessage(1181));
									continue;
								}
								}*/
							if(pc.getDollList().getDollByItem(item.getId()) !=null){
								pc.sendPackets(new S_ServerMessage(1181));
								continue;
							}
							if (targetPc.getInventory().checkAddItem(item, count) == L1Inventory.OK) { // 容量重量確認及びメッセージ送信
								for (int j = 0; j < count; j++) { // オーバーフローをチェック
									if (buyPrice * j > 2000000000) {
										targetPc.sendPackets(new S_ServerMessage(904, "2000000000"));// 総販売価格は%dアデナを超過できません。
										return;
									}
								}
								allPrice = count * buyPrice;

								if(pc.getMapId() == 370){ Money = 90308;}


								if (targetPc.getInventory().checkItem(Money,allPrice)) {
									adena = targetPc.getInventory().findItemId(Money);
									if (adena != null) {
										targetPc.getInventory().tradeItem(adena,allPrice, pc.getInventory());
										pc.getInventory().tradeItem(item, count,targetPc.getInventory());
									
										this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName())
										.append("		流水號:【  ").append(item.getId()).append(" 】	售價:	").append(allPrice).append("\r\n");
										if(!log){log=true;}
									
										psbl.setBuyCount(count + buyCount);
										buyList.set(order, psbl);
										if (psbl.getBuyCount() == psbl.getBuyTotalCount()) { // 買う予定の個数を買った
											isRemoveFromList[order] = true;
										}
									}
								} else {
									if(Money == L1ItemId.ADENA){
										targetPc.sendPackets(new S_ServerMessage(189)); // \f1アデナが不足しています。
									}else if(Money == 90308){
										targetPc.sendPackets(new S_SystemMessage("商城元寶不足。"));
									}
									break;
								}
							} else {
								pc.sendPackets(new S_ServerMessage(271)); // \f1相手が物を持ちすぎていて取引できません。
								break;
							}
							//opqlo 防洗
						} else {
							_log.info("[c]Result 防洗踢除 "+client.getAccountName());
							client.close();
							continue;
						}
						//opqlo 防洗
					}
					// 買い切ったアイテムをリストの末尾から削除
					for (int i = 7; i >= 0; i--) {
						if (isRemoveFromList[i]) {
							buyList.remove(i);
						}
					}
					targetPc.setTradingInPrivateShop(false);
					if(log){this.msg.append( "	擺攤玩家:【").append(targetPc.getName()).append("】	身上元寶:").append(targetPc.getInventory().findItemIdAndGetCount(90308)).append("\r\n")
						.append( "	對象玩家:【").append(pc.getName()).append("】	身上元寶:").append(pc.getInventory().findItemIdAndGetCount(90308)).append("\r\n");
					各項記錄輸出.LogSave(3,this.msg.toString());
					}
					this.msg.delete(0, this.msg.length());
				}
				break;
			case 2:
				/**個人倉庫存入*/
				if(npcImpl.equalsIgnoreCase("L1Dwarf") && level >= 5) {
					int objectId, count;
					this.msg.append("時間:	").append(new Timestamp(System.currentTimeMillis())).append("\r\n\r\n")
					.append(pc.getNetConnection().getIp().toString()).append( "	玩家:【").append(pc.getName()).append("】\r\n")
					.append("----------").append("【存入】清單如下:----------\r\n\r\n");
					boolean log =false;
					for (int i = 0; i < size; i++) {
						tradable = true;
						objectId = this.readD();
						count = this.readD();
						//opqlo 防洗
						count = Math.abs(count);
						count = Math.min(count,2000000000);
						count = Math.max(count,0);
						//opqlo 防洗
						final L1Object object = pc.getInventory().getItem(objectId);
						final L1ItemInstance item = (L1ItemInstance) object;
						if (item == null) {
							continue;
						}
						//opqlo 防洗
						if(count > 0 && count < 2000000000
								&& item.getCount() > 0 && item.getCount() >= count)
						{
							//opqlo 防洗

							if (!item.getItem().isTradable()) {
								tradable = false;
								pc.sendPackets(new S_ServerMessage(210, item.getItem().getName())); // \f1%0は捨てたりまたは他人に讓ることができません。
							}
							if (item.getDeleteDate() != null) {//opqlo 道具天數刪除系統(不可存倉)
								pc.sendPackets(new S_ServerMessage(210, item.getItem().getName()));
								tradable = false;
							}

							for (final L1NpcInstance petNpc : pc.getPetList().values()) {
								if (petNpc instanceof L1PetInstance) {
									final 	L1PetInstance pet = (L1PetInstance) petNpc;
									if (item.getId() == pet.getItemObjId()) {
										tradable = false;
										// \f1%0は捨てたりまたは他人に讓ることができません。
										pc.sendPackets(new S_ServerMessage(210, item.getItem().getName()));
										break;
									}
								}
							}
							//TODO 魔法娃娃使用判斷
							/*for (final L1DollInstance doll : pc.getDollList().values()) {
								if (item.getId() == doll.getItemObjId()) {
									tradable = false;
									pc.sendPackets(new S_ServerMessage(1181));
									break;
								}
							}*/
							if(pc.getDollList().getDollByItem(item.getId()) !=null){
								pc.sendPackets(new S_ServerMessage(1181));
								tradable = false;
								break;
							}

							if (pc.getDwarfInventory().checkAddItemToWarehouse(item, count,L1Inventory.WAREHOUSE_TYPE_PERSONAL) == L1Inventory.SIZE_OVER) {
								pc.sendPackets(new S_ServerMessage(75)); // \f1これ以上ものを置く場所がありません。
								break;
							}
							if (tradable) {
								pc.getInventory().tradeItem(objectId, count,pc.getDwarfInventory());
								pc.turnOnOffLight();
								if(item.getItem().isStackable()){
								this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName()).append(" 數量: ").append(count)
								.append("		流水號:【  ").append(item.getId()).append(" 】\r\n");
								}else{
									this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName())
									.append("		流水號:【  ").append(item.getId()).append(" 】\r\n");
								}
								if(!log){log=true;}
							}
							//opqlo 防洗
						} else {
							_log.info("[c]Result 防洗踢除 "+client.getAccountName());
							client.close();
							continue;
						}
						//opqlo 防洗
					}
					if(log){
						各項記錄輸出.LogSave(5,this.msg.toString());
					}
					this.msg.delete(0, this.msg.length());
				}
				break;
			case 3:
				/**個人倉庫提領*/
				if( npcImpl.equalsIgnoreCase("L1Dwarf") && level >= 5){
					int objectId;
					long count;
					L1ItemInstance item;
					this.msg.append("時間:	").append(new Timestamp(System.currentTimeMillis())).append("\r\n\r\n")
					.append(pc.getNetConnection().getIp().toString()).append( "	玩家:【").append(pc.getName()).append("】\r\n")
					.append("----------").append("【提領】清單如下:----------\r\n\r\n");
					boolean log =false;


					for (int i = 0; i < size; i++) {
						objectId = this.readD();
						count = this.readD();
						//opqlo 防洗
						count = Math.abs(count);
						count = Math.min(count,2000000000);
						count = Math.max(count,0);
						//opqlo 防洗
						item = pc.getDwarfInventory().getItem(objectId);
						if (item == null) {
							continue;
						}
						//opqlo 防洗
						if(count > 0 && count < 2000000000 && item.getCount() > 0 ){

							if( count > item.getCount()){
								count = item.getCount();
							}
							//opqlo 防洗
							if (pc.getInventory().checkAddItem(item, count) == L1Inventory.OK) // 容量重量確認及びメッセージ送信
							{
								if (pc.getInventory().consumeItem(L1ItemId.ADENA, 30)) {
									pc.getDwarfInventory().tradeItem(item, count,pc.getInventory());
									if(item.getItem().isStackable()){
										this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName()).append(" 數量: ").append(count)
										.append("		流水號:【  ").append(item.getId()).append(" 】\r\n");
										}else{
											this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName())
											.append("		流水號:【  ").append(item.getId()).append(" 】\r\n");
										}
									if(!log){log=true;}
								} else {
									pc.sendPackets(new S_ServerMessage(189)); // \f1アデナが不足しています。
									break;
								}
							} else {
								pc.sendPackets(new S_ServerMessage(270)); // \f1持っているものが重くて取引できません。
								break;
							}
							//opqlo 防洗
						} else {
							_log.info("[c]Result 防洗踢除 "+client.getAccountName());
							client.close();
							continue;
						}
						//opqlo 防洗
					}
					if(log){
						各項記錄輸出.LogSave(5,this.msg.toString());
					}
					this.msg.delete(0, this.msg.length());
				}
				break;

			case 4:
				/**血盟倉庫存入*/
				if(npcImpl.equalsIgnoreCase("L1Dwarf") && level >= 5) {
					if (pc.getClanid() != 0) { // クラン所属
						int objectId, count;
						this.msg.append("時間:	").append(new Timestamp(System.currentTimeMillis())).append("\r\n\r\n")
						.append(pc.getNetConnection().getIp().toString()).append("血盟:	").append(pc.getClanname()).append("	成員:【").append(pc.getName()).append("】\r\n")
						.append("----------").append("【存入】清單如下:----------\r\n\r\n");
						boolean log =false;
						final L1Clan clan = WorldClan.get().getClan(pc.getClanname());
						L1Object object =null;
						L1ItemInstance item = null;
						for (int i = 0; i < size; i++) {
							tradable = true;
							objectId = this.readD();
							count = this.readD();
							//opqlo 防洗
							count = Math.abs(count);
							count = Math.min(count,2000000000);
							count = Math.max(count,0);
							//opqlo 防洗


							object = pc.getInventory().getItem(objectId);
							item = (L1ItemInstance) object;
							if (item == null) {
								continue;
							}
							//opqlo 防洗
							if(count > 0 && count < 2000000000
									&& item.getCount() > 0 && item.getCount() >= count)
							{
								//opqlo 防洗
								if (clan != null) {
									if (!item.getItem().isTradable()) {
										tradable = false;
										pc.sendPackets(new S_ServerMessage(210, item.getItem().getName())); // \f1%0は捨てたりまたは他人に讓ることができません。
									}
									if (item.getDeleteDate() != null) {//opqlo 道具天數刪除系統(不可存倉)
										pc.sendPackets(new S_ServerMessage(210, item.getItem().getName()));
										tradable = false;
									}
									if (item.getBless() >= 128) { // 封印された装備
										tradable = false;
										pc.sendPackets(new S_ServerMessage(210, item.getItem().getName())); // \f1%0は捨てたりまたは他人に讓ることができません。
									}
									for (final L1NpcInstance petNpc : pc.getPetList().values()) {
										if (petNpc instanceof L1PetInstance) {
											final L1PetInstance pet = (L1PetInstance) petNpc;
											if (item.getId() == pet.getItemObjId()) {
												tradable = false;
												// \f1%0は捨てたりまたは他人に讓ることができません。
												pc.sendPackets(new S_ServerMessage(210, item.getItem().getName()));
												break;
											}
										}
									}
									/*
									//TODO 魔法娃娃使用判斷
									for (final L1DollInstance doll : pc.getDollList().values()) {
										if (item.getId() == doll.getItemObjId()) {
											tradable = false;
											pc.sendPackets(new S_ServerMessage(1181));
											break;
										}
									}*/
									if(pc.getDollList().getDollByItem(item.getId()) !=null){
										pc.sendPackets(new S_ServerMessage(1181));
										tradable = false;
										break;
									}
									if (clan.getDwarfForClanInventory().checkAddItemToWarehouse(item, count,L1Inventory.WAREHOUSE_TYPE_CLAN) == L1Inventory.SIZE_OVER) {
										pc.sendPackets(new S_ServerMessage(75)); // \f1これ以上ものを置く場所がありません。
										break;
									}
									if (tradable) {
										pc.getInventory().tradeItem(objectId, count,clan.getDwarfForClanInventory());
										pc.turnOnOffLight();
										if(item.getItem().isStackable()){
											this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName()).append(" 數量: ").append(count)
											.append("		流水號:【  ").append(item.getId()).append(" 】\r\n");
											}else{
												this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName())
												.append("		流水號:【  ").append(item.getId()).append(" 】\r\n");
											}
										if(!log){log=true;}

									}
									//opqlo 防洗
								} else {
									_log.info("[c]Result 防洗踢除 "+client.getAccountName());
									client.close();
									continue;
								}
								//opqlo 防洗
							}
						}
						if(log){
							各項記錄輸出.LogSave(6,this.msg.toString());
						}
					} else {
						pc.sendPackets(new S_ServerMessage(208)); // \f1血盟倉庫を使用するには血盟に加入していなくてはなりません。
					}
					this.msg.delete(0, this.msg.length());
				}
				break;
			case 5:
				/**血盟倉庫取出*/
				if(npcImpl.equalsIgnoreCase("L1Dwarf") && level >= 5) {
					final L1Clan clan = WorldClan.get().getClan(pc.getClanname());
					if (clan != null) {
						int objectId, count;
						L1ItemInstance item;
						this.msg.append("時間:	").append(new Timestamp(System.currentTimeMillis())).append("\r\n\r\n")
						.append(pc.getNetConnection().getIp().toString()).append("血盟:	").append(pc.getClanname()).append("	成員:【").append(pc.getName()).append("】\r\n")
						.append("----------").append("【提領】清單如下:----------\r\n\r\n");
						boolean log =false;

						for (int i = 0; i < size; i++) {
							objectId = this.readD();
							count = this.readD();

							//opqlo 防洗
							count = Math.abs(count);
							count = Math.min(count,2000000000);
							count = Math.max(count,0);
							//opqlo 防洗

							item = clan.getDwarfForClanInventory().getItem(objectId);
							if (item == null) {
								continue;
							}
							//opqlo 防洗
							if(count > 0 && count < 2000000000
									&& item.getCount() > 0 && item.getCount() >= count)
							{
								//opqlo 防洗

								if (pc.getInventory().checkAddItem(item, count) == L1Inventory.OK) { // 容量重量確認及びメッセージ送信
									if (pc.getInventory().consumeItem(L1ItemId.ADENA, 30)) {
										clan.getDwarfForClanInventory().tradeItem(item,count, pc.getInventory());
										if(item.getItem().isStackable()){
											this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName()).append(" 數量: ").append(count)
											.append("		流水號:【  ").append(item.getId()).append(" 】\r\n");
											}else{
												this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName())
												.append("		流水號:【  ").append(item.getId()).append(" 】\r\n");
											}
										if(!log){log=true;}
									} else {
										pc.sendPackets(new S_ServerMessage(189)); // \f1アデナが不足しています。
										break;
									}
								} else {
									pc.sendPackets(new S_ServerMessage(270)); // \f1持っているものが重くて取引できません。
									break;
								}
								//opqlo 防洗
							} else {
								_log.info("[c]Result 防洗踢除 "+client.getAccountName());
								client.close();
								continue;
							}
							//opqlo 防洗
						}
						clan.setWarehouseUsingChar(0); // クラン倉庫のロックを解除
						if(log){
							各項記錄輸出.LogSave(6,this.msg.toString());
						}
					}
					this.msg.delete(0, this.msg.length());
				}
				break;

			case 8:
				/**妖精倉庫存入*/
				if(npcImpl.equalsIgnoreCase("L1Dwarf") && level >= 5 && pc.isElf()) {
					int objectId, count;
					this.msg.append("時間:	").append(new Timestamp(System.currentTimeMillis())).append("\r\n\r\n")
					.append(pc.getNetConnection().getIp().toString()).append( "	玩家:【").append(pc.getName()).append("】\r\n")
					.append("----------").append("【存入】清單如下:----------\r\n\r\n");
					boolean log =false;

					for (int i = 0; i < size; i++) {
						tradable = true;
						objectId = this.readD();
						count = this.readD();

						//opqlo 防洗
						count = Math.abs(count);
						count = Math.min(count,2000000000);
						count = Math.max(count,0);
						//opqlo 防洗

						final L1Object object = pc.getInventory().getItem(objectId);
						final L1ItemInstance item = (L1ItemInstance) object;
						if (item == null) {
							continue;
						}
						//opqlo 防洗
						if(count > 0 && count < 2000000000
								&& item.getCount() > 0 && item.getCount() >= count)
						{
							//opqlo 防洗

							if (!item.getItem().isTradable()) {
								tradable = false;
								pc.sendPackets(new S_ServerMessage(210, item.getItem().getName())); // \f1%0は捨てたりまたは他人に讓ることができません。
							}
							for (final L1NpcInstance petNpc : pc.getPetList().values()) {
								if (petNpc instanceof L1PetInstance) {
									final L1PetInstance pet = (L1PetInstance) petNpc;
									if (item.getId() == pet.getItemObjId()) {
										tradable = false;
										// \f1%0は捨てたりまたは他人に讓ることができません。
										pc.sendPackets(new S_ServerMessage(210, item.getItem().getName()));
										break;
									}
								}
							}
							//TODO 魔法娃娃使用判斷
							if(pc.getDollList().getDollByItem(item.getId()) !=null){
								tradable = false;
								pc.sendPackets(new S_ServerMessage(1181));
							}
							/*	for (final L1DollInstance doll : pc.getDollList().values()) {
								if (item.getId() == doll.getItemObjId()) {
									tradable = false;
									pc.sendPackets(new S_ServerMessage(1181));
									break;
								}
							}*/
							//TODO 魔法娃娃使用判斷
							if (pc.getDwarfForElfInventory().checkAddItemToWarehouse(item,
									count, L1Inventory.WAREHOUSE_TYPE_PERSONAL) == L1Inventory.SIZE_OVER) {
								pc.sendPackets(new S_ServerMessage(75)); // \f1これ以上ものを置く場所がありません。
								break;
							}
							if (tradable) {
								pc.getInventory().tradeItem(objectId, count,pc.getDwarfForElfInventory());
								pc.turnOnOffLight();
								if(item.getItem().isStackable()){
									this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName()).append(" 數量: ").append(count)
									.append("		流水號:【  ").append(item.getId()).append(" 】\r\n");
									}else{
										this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName())
										.append("		流水號:【  ").append(item.getId()).append(" 】\r\n");
									}
								if(!log){log=true;}
							}
							//opqlo 防洗
						} else {
							_log.info("[c]Result 防洗踢除 "+client.getAccountName());
							client.close();
							continue;
						}
						//opqlo 防洗
					}
					if(log){
						各項記錄輸出.LogSave(4,this.msg.toString());
					}
					this.msg.delete(0, this.msg.length());
				}
				break;

			case 9:
				/**妖精倉庫取出*/
				if(npcImpl.equalsIgnoreCase("L1Dwarf") && level >= 5 && pc.isElf()) {
					int objectId, count;
					this.msg.append("時間:	").append(new Timestamp(System.currentTimeMillis())).append("\r\n\r\n")
					.append(pc.getNetConnection().getIp().toString()).append( "	玩家:【").append(pc.getName()).append("】\r\n")
					.append("----------").append("【提領】清單如下:----------\r\n\r\n");
					boolean log =false;
					L1ItemInstance item;
					for (int i = 0; i < size; i++) {
						objectId = this.readD();
						count = this.readD();

						//opqlo 防洗
						count = Math.abs(count);
						count = Math.min(count,2000000000);
						count = Math.max(count,0);
						//opqlo 防洗

						item = pc.getDwarfForElfInventory().getItem(objectId);
						if (item == null) {
							continue;
						}
						//opqlo 防洗
						if(count > 0 && count < 2000000000
								&& item.getCount() > 0 && item.getCount() >= count)
						{
							//opqlo 防洗

							if (pc.getInventory().checkAddItem(item, count) == L1Inventory.OK) { // 容量重量確認及びメッセージ送信
								if (pc.getInventory().consumeItem(40494, 2)) { // ミスリル
									pc.getDwarfForElfInventory().tradeItem(item, count,pc.getInventory());
									if(item.getItem().isStackable()){
										this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName()).append(" 數量: ").append(count)
										.append("		流水號:【  ").append(item.getId()).append(" 】\r\n");
										}else{
											this.msg.append("[").append(item.getItemId()).append("]		").append(item.getViewName())
											.append("		流水號:【  ").append(item.getId()).append(" 】\r\n");
										}
									if(!log){log=true;}
								} else {
									pc.sendPackets(new S_ServerMessage(337, "$767")); // \f1%0が不足しています。
									break;
								}
							} else {
								pc.sendPackets(new S_ServerMessage(270)); // \f1持っているものが重くて取引できません。
								break;
							}
							//opqlo 防洗
						} else {
							_log.info("[c]Result 防洗踢除 "+client.getAccountName());
							client.close();
							continue;
						}
						//opqlo 防洗
					}
					if(log){
						各項記錄輸出.LogSave(4,this.msg.toString());
					}
					this.msg.delete(0, this.msg.length());
				}
				break;
			}
			if(this.msg !=null){
				this.msg.delete(0, this.msg.length());
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}

}
